#!/bin/bash

_usage(){
	echo -e "\

A simple script to pull topic repositories for use as Ciel local repositories.

Usage:

	pull-topic-as-local [TOPIC_NAME_A] [TOPIC_NAME_B] ... [TOPIC_NAME_Z]
"

}

if [ -z $1 ]; then
	_usage
fi

for i in $@; do
	if [ -e OUTPUT-$i ]; then
		while true; do
			read -p "Detected an existing local repository for $i, overwrite? " yn
			case $yn in
				[Yy]* )
					rm -r OUTPUT-$i; break ;;
				[Nn]* )
					echo "Aborting, please move your local repository directory OUTPUT-$i aside."; exit 1 ;;
			        * )
					echo "Please input Y[y] or N[n]." ;;
			esac
		done
	fi

	echo "Pulling topic repository: $i ..."
	rsync -avSHP \
		rsync://mirror.anthonos.org/anthon/debs/pool/$i/ \
		OUTPUT-$i

	echo "Adjusting topic repository for Ciel: $i ..."
	mv -v OUTPUT-$i/{main,debs}
done
